﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import flash.utils.*;
    import flash.net.*;
    import Engine.*;

    public class LocalizationManager {

        private static var m_xml:XML;
        private static var m_callBackFunc:Function;
        private static var m_urlLoader:URLLoader;
        private static var m_dictionary:Dictionary;
        private static var m_fileIsCompressed:Boolean;

        public static function init(_arg1:String, _arg2:Function, _arg3:Boolean=false):void{
            m_fileIsCompressed = _arg3;
            m_urlLoader = new URLLoader();
            if (m_fileIsCompressed){
                m_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            };
            m_urlLoader.addEventListener(Event.COMPLETE, xmlParseCallBack);
            m_callBackFunc = _arg2;
            m_urlLoader.load(new URLRequest(_arg1));
        }
        public static function xmlParseCallBack(_arg1:Event):void{
            var _local2:XML;
            var _local3:String;
            m_urlLoader.removeEventListener(Event.COMPLETE, xmlParseCallBack);
            if (m_fileIsCompressed){
                _local3 = Utilities.uncompress(_arg1.target.data);
                _local2 = new XML(_local3);
            } else {
                GlobalEngine.msg("Loaded uncompressed localization file. Size:", _arg1.target.data.length);
                _local2 = new XML(_arg1.target.data);
            };
            m_xml = _local2;
            LocalizationManager.setLocaleXml(m_xml);
            m_dictionary = new Dictionary();
            m_callBackFunc();
        }
        public static function setLocaleXml(_arg1:XML):void{
            m_xml = _arg1;
        }
        private static function getLocalizationString(_arg1:String, _arg2:String):String{
            var hashKey:* = null;
            var bundleList:* = null;
            var bundleXML:* = null;
            var bundleLookup:* = null;
            var bundleName:* = _arg1;
            var resource:* = _arg2;
            var stringResult:* = null;
            if (m_xml){
                hashKey = ((bundleName + ":") + resource);
                stringResult = m_dictionary[hashKey];
                if (stringResult == null){
                    bundleList = m_xml.bundle.(@name == bundleName);
                    if (bundleList){
                        bundleXML = bundleList[0];
                        if (((!((bundleXML == null))) && (bundleXML.bundleLine))){
                            bundleLookup = bundleXML.bundleLine.(@key == resource);
                            if (bundleLookup){
                                stringResult = bundleLookup.value;
                            };
                            m_dictionary[hashKey] = stringResult;
                        };
                    };
                };
                if (stringResult == ""){
                    stringResult = null;
                };
            };
            return (stringResult);
        }
        public static function hasLocalizationString(_arg1:String, _arg2:String):Boolean{
            return (!((getLocalizationString(_arg1, _arg2) == null)));
        }
        public static function quickLocalize(_arg1:String, _arg2:String, _arg3:Array=null):String{
            var _local5:RegExp;
            var _local6:int;
            var _local4:String = getLocalizationString(_arg1, _arg2);
            if (_local4){
                if (_arg3){
                    _local6 = 0;
                    while (_local6 < _arg3.length) {
                        if (_local4){
                            _local5 = new RegExp((("\\{" + _local6) + "\\}"), "gim");
                            _local4 = _local4.replace(_local5, _arg3[_local6]);
                        };
                        _local6++;
                    };
                };
            } else {
                if (Config.DEBUG_MODE == true){
                    _local4 = ((("Could not find localization data for " + _arg1) + ":") + _arg2);
                    GlobalEngine.log("localization", ((("bundleName: " + _arg1) + " resource: ") + _arg2));
                } else {
                    _local4 = "";
                };
            };
            return (_local4);
        }

    }
}//package Engine.Managers 
